﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using RMS.View;

namespace RMS.Controller
{
    public class LoginController
    {
        private MainWindow _parentForm;
        private Login _loginView;

        public LoginController(MainWindow parent, Login view)
        {
            this._parentForm = parent;
            this._loginView = view;
            this._parentForm.DirectorMenu = new DirectorMenuView();
            this._parentForm.DirectorMenu.Visible = false;
            this._parentForm.Controls.Add(this._parentForm.DirectorMenu);
        }

        public Login View
        {
            get { return this._loginView; }
            set { this._loginView = value; }
        }

        public void HandleLoginButtonClick(string p1, string p2)
        {
            if(p1 == "") {
#if DEBUG
                throw new ArgumentNullException();
#else
                MessageBox.Show("empty username");
                return;
#endif
            }
            if(p2 == "") {
#if DEBUG
                throw new ArgumentNullException();
#else
                MessageBox.Show("empty password");
                return;
#endif
            }
            int auth;
            if ((auth = Persistence.DataManager.Instance.Login(p1, p2)) >= 0)
            {
                int id = Persistence.DataManager.Instance.GetID(p1);
                switch (auth)
                {
                    case 1: //DIRECTOR
                        int departmentId = Persistence.DataManager.Instance.GetDepartmentID(id);
                        //this._parentForm.DirectorMenu.Visible = true;

                        
                        //View.AcademicDepartmentView departmentView = new View.AcademicDepartmentView();
                        //Model.AcademicDepartment departmentModel = new Model.AcademicDepartment(id, "", "");
                        //Controller.AcademicDepartmentController departmentController = new AcademicDepartmentController(departmentModel, departmentView);
                        
                        //departmentView.Controller = departmentController;
                        //departmentController.SyncModelWithDatabase(departmentId);
                        //departmentController.View.RefreshHumanResourcesList(departmentController.Model.HumanResources);

                        //this._parentForm.Text = string.Format("Academic department [{0}][{1}]", departmentController.Model.Id, departmentController.Model.Name);
                        //this._parentForm.Panel.Size = new System.Drawing.Size(departmentView.Size.Width, departmentView.Size.Height);
                        //this._parentForm.Size = new System.Drawing.Size(this._parentForm.Panel.Size.Width + 46, this._parentForm.Panel.Size.Height + 86);
                        //this._parentForm.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2 - departmentView.Size.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - departmentView.Size.Height / 2);
                        //this._parentForm.Panel.Controls.Add(departmentView);
                        //this._loginView.Hide();
                        
                        

                        //view.academicprogramview programview = new academicprogramview();
                        //model.academicprogram programmodel = new model.academicprogram(0, "", 0, model.academicprogramtype.master);
                        //controller.academicprogramcontroller programcontroller = new academicprogramcontroller(programmodel, programview);
                        //programview.controller = programcontroller;
                        //programcontroller.syncmodelwithdatabase();
                        //programcontroller.view.populateprogramlist(programcontroller.programlist);

                        //this._parentForm.Text = "Academic programs";
                        //this._parentForm.Panel.Size = new System.Drawing.Size(programView.Size.Width, programView.Size.Height);
                        //this._parentForm.Size = new System.Drawing.Size(this._parentForm.Panel.Size.Width + 46, this._parentForm.Panel.Size.Height + 86);
                        //this._parentForm.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2 - programView.Size.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - programView.Size.Height / 2);
                        //this._parentForm.Panel.Controls.Add(programView);
                        //this._loginView.Hide();                        


                        View.ResearchProjectView researchView = new View.ResearchProjectView();
                        Controller.ResearchProjectController researchController = new Controller.ResearchProjectController(researchView);
                        researchView.Controller = researchController;
                        researchController.InitListOfProjects();
                        researchController.View.PopulateProjectList(researchController.Model);

                        this._parentForm.Text = "Research Projects";
                        this._parentForm.Panel.Size = new System.Drawing.Size(researchView.Size.Width, researchView.Size.Height);
                        this._parentForm.Size = new System.Drawing.Size(this._parentForm.Panel.Size.Width + 46, this._parentForm.Panel.Size.Height + 86);
                        this._parentForm.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2 - researchView.Size.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - researchView.Size.Height / 2);
                        this._parentForm.Panel.Controls.Add(researchView);
                        this._loginView.Hide();


                        //View.StudentResearchGroupView groupView = new StudentResearchGroupView();
                        //List<Model.StudentResearchGroup> groupModel = new List<Model.StudentResearchGroup>();
                        //Controller.StudentResearchGroupController groupController = new StudentResearchGroupController(groupView);
                        //groupView.Controller = groupController;
                        //groupController.Model = Persistence.DataManager.Instance.SyncStudentResearchGroups();
                        //groupController.View.PopulateGroupList(groupController.Model);
                        //groupController.View.PopulateStudentList(Persistence.DataManager.Instance.SyncStudentList());

                        //this._parentForm.Text = "Student Research Groups";
                        //this._parentForm.Panel.Size = new System.Drawing.Size(groupView.Size.Width, groupView.Size.Height);
                        //this._parentForm.Size = new System.Drawing.Size(this._parentForm.Panel.Size.Width + 46, this._parentForm.Panel.Size.Height + 86);
                        //this._parentForm.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2 - groupView.Size.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - groupView.Size.Height / 2);
                        //this._parentForm.Panel.Controls.Add(groupView);
                        //this._loginView.Hide();

                        break;
                    case 2: //TEACHER
                        Model.Teacher teacher = new Model.Teacher();
                        teacher.Id = id;
                        AcademicDutyView dutyView = new AcademicDutyView();
                        AcademicDutyController dutyController = new AcademicDutyController(teacher, dutyView);
                        dutyView.Controller = dutyController;
                        dutyController.SyncTeacher();
                        this._parentForm.Text = string.Format("Academic duties [{0}][{1}]", dutyController.Model.Id.ToString(), dutyController.Model.Username);

                        this._parentForm.Panel.Size = new System.Drawing.Size(dutyView.Size.Width, dutyView.Size.Height);
                        this._parentForm.Size = new System.Drawing.Size(this._parentForm.Panel.Size.Width + 46, this._parentForm.Panel.Size.Height + 86);
                        this._parentForm.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2 - dutyView.Size.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - dutyView.Size.Height / 2);
                        this._parentForm.Panel.Controls.Add(dutyView);                        
                        this._loginView.Hide();
                        break;
                    case 3: //SECRETARY
                        break;
                    case 4: //STUDENT
                        break;
                }
            }
            else
                System.Windows.Forms.MessageBox.Show("no");
        }
    }
}
